package controller.expertInfo.expertEdit;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.CertificateTypeServices;
import services.expertInfo.ExpertTrainServices;
import util.JsonUtil;
import validator.EndTimeFluentValidator;
import validator.NotBlankFluentValidator;
import validator.NotNullFluentValidator;
import wt.entity.expertInfo.ExpertTrain;

import java.util.List;
import java.util.Map;

;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: expert
 */
@Controller
@RequestMapping("expertInfo/expertEdit/expertTrain")
public class ExpertSelfTrainController extends BaseController {

    @Autowired
    ExpertTrainServices expertTrainServices;
    @Autowired
    CertificateTypeServices certificateTypeServices;

    @RequiresPermissions("expertInfo:expertEdit:default")
    @RequestMapping("index")
    public String index( Map map) throws Exception {

        List<ExpertTrain> expertTrainList = expertTrainServices.expertSelectByExpertId(getLoginAccount().getExpertBase().getId());
        map.put("data", JsonUtil.obj2Json(expertTrainList));
        return "expertInfo/expertEdit/expertTrain/index";
    }


    @RequiresPermissions("expertInfo:expertEdit:default")
    @RequestMapping("editDlg")
    public String editDlg(String id,  Map map) throws Exception {
        if (StringUtils.isNotEmpty(id)) {
            ExpertTrain expertTrain= expertTrainServices.selectByPrimaryKey(id);
            if (expertTrain == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG;
            }
            map.put(StaticConstant.EDIT_DATA, expertTrain);
            map.put(StaticConstant.EDIT_URL, "expertInfo/expertEdit/expertTrain/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        } else {
            map.put(StaticConstant.EDIT_URL, "expertInfo/expertEdit/expertTrain/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        }
        return "expertInfo/expertEdit/expertTrain/editDlg";
    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(ExpertTrain expertTrain) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertTrain.getName(), new NotBlankFluentValidator("培训名称"))
                .on(expertTrain.getEndTime(), new EndTimeFluentValidator(expertTrain.getStartTime(), "开始时间", "结束时间", false))
                .on(expertTrain.getSignState(), new NotNullFluentValidator("签到情况"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        if (expertTrain.getSignState() == 1) {
            expertTrain.setReason("");
        }

        expertTrain.setExpertId(getLoginAccount().getExpertBase().getId());

        expertTrainServices.expertInsertSelective(expertTrain);

        return commonResult.setSuccess();
    }


    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(ExpertTrain expertTrain) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertTrain.getName(), new NotBlankFluentValidator("培训名称"))
                .on(expertTrain.getEndTime(), new EndTimeFluentValidator(expertTrain.getStartTime(), "开始时间", "结束时间", false))
                .on(expertTrain.getSignState(), new NotNullFluentValidator("签到情况"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        if (expertTrain.getSignState() == 1) {
            expertTrain.setReason("");
        }
        expertTrain.setExpertId(getLoginAccount().getExpertBase().getId());


        expertTrainServices.expertUpdateByPrimaryKeySelective(expertTrain);

        return commonResult.setSuccess();
    }


    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(id)) {
            commonResult.addMessage(StaticConstant.DATA_IS_BLANK);
            return commonResult.setError();
        }
        expertTrainServices.expertDeleteByPrimaryKey(id);

        return commonResult.setSuccess();
    }



    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("cancelApply")
    public CommonResult cancelApply(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        int result=  expertTrainServices.expertCancelApplyByPrimaryKey(id);
        commonResult.setStatus(result);
        return commonResult;
    }

}
